package com.situ.be.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.be.entity.BeOrderuser;
import com.situ.be.entity.BeRequest1;
import com.situ.be.service.BeOrderuserService;
import com.situ.sy.entity.SyMetertype;
import com.situ.sy.service.SyMetertypeService;
import com.situ.test.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.situ.be.service.BeOrderService;
import com.situ.be.entity.BeOrder;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * BeOrder的路由接口服务
 * 
 * @author胡文
 *
 */
//
@Api(description = "BeOrder模块接口说明")
@RestController
public class BeOrderController {

	/** BeOrderService服务 */
	@Autowired
	private BeOrderService beOrderService;

	@Autowired
	private BeOrderuserService orderuserService;

	@Autowired
	private SyMetertypeService metertypeService;
	
	/**
	 * 查询所有BeOrder数据的方法
	 * @param value
	 * @return
	 */
@ApiOperation("查询所有BeOrder数据的方法")
	@GetMapping(value = "/BeOrder/query", produces = {"application/json;charset=UTF-8"})
	public ResponseData query(BeOrder value) {

		return ResponseData.ok().putDataValue("list",beOrderService.find(value));
	}
	
	/**
	 * 通过id查询BeOrder数据的方法
	 * 
	 * @return
	 */
     @ApiOperation("通过id查询BeOrder数据的方法")
	@GetMapping(value = "/BeOrder/queryById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData queryById(@PathVariable(name="id") String id) {
		 return ResponseData.ok().putDataValue("obj",beOrderService.findOne(id));
	}
	
	/**
	 * 插入BeOrder属性不为空的数据方法
	 * 
	 * @return
	 */
@ApiOperation("插入BeOrder属性不为空的数据方法")
	@PostMapping(value = "/BeOrder/save")
	public ResponseData save(BeOrder value) {
		return ResponseData.ok().putDataValue("obj", beOrderService.saveNotNull(value));
	}
	
	/**
	 * 更新BeOrder属性不为空的数据方法
	 * 
	 * @return
	 */
     @ApiOperation("更新BeOrder属性不为空的数据方法")
	@PostMapping(value = "/BeOrder/update", produces = {"application/json;charset=UTF-8"})
	public ResponseData update(BeOrder value) {
		return ResponseData.ok().putDataValue("obj",beOrderService.updateNotNullById(value));
	}

	/**
	 * 通过id删除BeOrder数据方法
	 * 
	 * @return
	 */
     @ApiOperation("通过id删除BeOrder数据方法")
	@PostMapping(value = "/BeOrder/delById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData delById(@PathVariable(name="id") String id) {
		return ResponseData.ok().putDataValue("obj",beOrderService.deleteById(id));
	}

    //带分页的查询全部
    @ApiOperation("带分页的条件模糊查询")
    @GetMapping(value = "/BeOrder/queryPage", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryPage(BeOrder value,
                            @ApiParam("第几页")
                             @RequestParam(defaultValue = "1",required = false) int pageNum ,
                                  @ApiParam("每页多少条")
                             @RequestParam(defaultValue = "10",required = false) int pageSize) {

	 PageHelper.startPage(pageNum, pageSize);
        List<BeOrder> list = beOrderService.find2(value);
    PageInfo pageInfo = new PageInfo<BeOrder>(list,7);
        return ResponseData.ok()
        .putDataValue("pageInfo",pageInfo);
   }

	@PostMapping(value = "/BeOrder/addOrder")
	public ResponseData addOrder(@RequestBody BeRequest1 order){
		SyMetertype syMetertype = metertypeService.findOne(order.getOrder().getMeterTypeID());
		LocalDate date = LocalDate.now();
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
		String i = dtf.format(date);
		order.getOrder().setOrderNo("B1-"+i+"0001");

		order.getOrder().setMeterTypeID(Integer.valueOf(syMetertype.getMeterTypeName().substring(2)));
		order.getOrder().setHouseHeight(Integer.valueOf(order.getOrder().getHouseHeight()));
		order.getOrder().setMeterCount(Integer.valueOf(order.getOrder().getMeterCount()));

		for (int j = 0; j < order.getBeOrderuserList().size(); j++) {
			order.getBeOrderuserList().get(j).setOrderNo("B1-"+i+"0001");
			orderuserService.saveNotNull(order.getBeOrderuserList().get(j));
		}

		return ResponseData.ok().putDataValue("obj",beOrderService.saveNotNull(order.getOrder()));
	}

}
